package edu.qd.learn.newchapter.chapter01;

/**
 * @author 布衣
 */
public class Math {

    public int deal(int x, int y, Operation op) {
        if (op == Operation.sum) {
            return sum(x, y);
        }
        if (op == Operation.sub) {
            return sub(x, y);
        }
        if (op == Operation.multiply) {
            return multiply(x, y);
        }
        if (op == Operation.divide) {
            return divide(x, y);
        }
        return 0;
    }



    /**
     * ... 可以0个也可以无穷 2 31-1
     *
     * @param x
     * @return
     */
    private int sum(int... x) {
        int sum = 0;
        for (int i = 0, len = x.length - 1; i < len; i++, len--) {
            sum += (x[i] + x[len]);
        }
        return sum + (x.length % 2 == 0 ? 0 : x[x.length >> 1]);
    }

    private int sub(int x, int y) {
        return x - y;
    }


    private int multiply(int x, int y) {
        return x * y;
    }


    private int divide(int x, int y) {
        return x / y;
    }

}
